package s3

import "github.com/pkg/errors"

const DefaultExpire = uint64(604740)

const (
	StorageTypeCommon = iota
	StorageTypeVolc
)

type Config struct {
	EndPoint    string
	Region      string
	Bucket      string
	AccountId   string
	AccessKey   string
	SecretKey   string
	Expire      uint64 //second
	StorageType int
}

func (c *Config) Valid() error {
	if c.StorageType == StorageTypeCommon {
		if c.AccessKey == "" {
			return errors.New("AccessKey is empty")
		}
		if c.SecretKey == "" {
			return errors.New("SecretKey is empty")
		}
	}

	if c.StorageType == StorageTypeVolc {
		if c.AccountId == "" {
			return errors.New("AccountID is empty")
		}

	}

	if c.Region == "" {
		return errors.New("region is empty")
	}
	if c.EndPoint == "" {
		return errors.New("end point is empty")
	}
	if c.Bucket == "" {
		return errors.New("bucket is empty")
	}

	if c.Expire <= 0 {
		c.Expire = DefaultExpire
	}

	return nil
}

type Option func(c *Config)

func WithVolcConfig(cc *VolcConfig) Option {
	return func(c *Config) {
		c.StorageType = StorageTypeVolc
		c.EndPoint = cc.EndPoint
		c.Region = cc.Region
		c.Bucket = cc.Bucket
		c.AccountId = cc.AccountId
		c.Expire = cc.Expire
	}
}

func WithCommonConfig(cc *CommonConfig) Option {
	return func(c *Config) {
		c.StorageType = StorageTypeCommon
		c.EndPoint = cc.EndPoint
		c.Region = cc.Region
		c.Bucket = cc.Bucket
		c.AccessKey = cc.AccessKey
		c.SecretKey = cc.SecretKey
		c.Expire = cc.Expire
	}
}

type VolcConfig struct {
	AccountId string
	Region    string
	EndPoint  string
	Bucket    string
	Expire    uint64
}

type CommonConfig struct {
	Region    string
	EndPoint  string
	AccessKey string
	SecretKey string
	Bucket    string
	Expire    uint64
}
